<template>
  <el-dialog width="60%" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '140px' : '90px'">
        <el-col :span="24">
          <el-col :span="12">
            <!-- 数据源代码 -->
            <el-form-item :label="$t('genDataSource.dataSourceCode')" prop="dataSourceCode">
              <!--文本框-->
              <el-input v-model="dataForm.dataSourceCode" :disabled="id_disabled"
                        :placeholder="$t('genDataSource.dataSourceCode')" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="12">
            <!-- 数据源名称 -->
            <el-form-item :label="$t('genDataSource.dataSourceName')" prop="dataSourceName">
              <!--文本框-->
              <el-input v-model="dataForm.dataSourceName"
                        :placeholder="$t('genDataSource.dataSourceName')" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="12">
            <!-- 驱动名称 -->
            <el-form-item :label="$t('genDataSource.driverClassName')" prop="driverClassName">
              <!--文本框-->
              <el-select
                v-model="dataForm.driverClassName"
                placeholder="请选择"
                clearable
                @change="checkDriverClass">
                <el-option
                  v-for="item in driverClassList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <!-- 连接串 -->
          <el-form-item :label="$t('genDataSource.dataSourceUrl')" prop="dataSourceUrl">
            <!--文本框-->
            <el-input v-model="dataForm.dataSourceUrl" type="textarea"
                      :rows="3"
                      :placeholder="$t('genDataSource.dataSourceUrl')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-col :span="12">
            <!-- 用户名 -->
            <el-form-item :label="$t('genDataSource.userName')" prop="userName">
              <!--文本框-->
              <el-input v-model="dataForm.userName"
                        :placeholder="$t('genDataSource.userName')" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="12">
            <!-- 密码 -->
            <el-form-item :label="$t('genDataSource.password')" prop="password">
              <!--文本框-->
              <el-input v-model="dataForm.password" show-password
                        :placeholder="$t('genDataSource.password')" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      id_disabled: false,
      driverClassList: [],
      dataForm: {
        id: '',
        dataSourceName: '',
        dataSourceCode: '',
        jdbcCode: '',
        driverClassName: 'com.mysql.jdbc.Driver',
        dataSourceUrl: '',
        userName: '',
        password: '',
        remark: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        dataSourceName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dataSourceCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        driverClassName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dataSourceUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.getDriverClassList().then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.driverClassList = res.data
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.loading = true
            this.getInfo()
            this.id_disabled = true
          } else {
            this.id_disabled = false
            this.checkDriverClass(this.dataForm.driverClassName)
          }
        })
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/gen/genDataSource/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 获取数据库驱动连接信息
    getDriverClassList () {
      return this.$http.get(`/gen/genDataSource/getDriverClassList`)
    },
    // 切换数据库类型
    checkDriverClass (val) {
      let temp = this.$xeutils.find(this.driverClassList, item => item.value === val)
      if (temp) {
        this.dataForm.dataSourceUrl = temp.urlDemo
      }
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/gen/genDataSource/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
